/*
 * @Description:
 * @Version: 2.0
 * @Autor: wangmiao
 * @Date: 2022-08-26 13:33:32
 * @LastEditors: wangmiao
 * @LastEditTime: 2022-08-26 14:10:29
 */
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { RootState } from '../index'
// 定义 初始state

interface CounterState {
  value: number // 规定value必须是number型
}

const initialState: CounterState = {
  value: 0,
}

export const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1
    },
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload
    },
  },
})

export const { increment, incrementByAmount } = counterSlice.actions

export const selectCount = (state: RootState) => state.counter.value

export default counterSlice.reducer
